package thelist;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import database.Database;

public class thelistUsers {
	
	static public int getUsersCount() {
		try {
			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(
						"select count(*) " +
						"from usr");
			
			rs.next();
			
			int count = rs.getInt(1);
			
			stmt.close();
			con.close();
			
			return count;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	static public void getUsers(PageContext p) {
		JspWriter out = p.getOut();
		
		try {
			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(
						"select * " +
						"from usr");
			
			while ( rs.next() ) {
				out.println(
					"<form class='usr' method='post' action='UpdateAccount'>" + "\n" +
					"	<div style='float:left; margin:15px;'>" + "\n" +
					"		<input type='hidden' name='id' value='" + rs.getInt("id") + "' />" + "\n" +
					"		<textarea cols='40' rows='1' wrap='off'" + "\n" +
					"			readonly='readonly' title='username'>" + rs.getString("login") + "</textarea>" + "\n" +
					"		<textarea cols='7' rows='1' wrap='off'" + "\n" +
					"			name='credit' title='credit'>" + rs.getInt("credit") + "</textarea>" + "\n" +
					"		<br />" + "\n" +
					"		<textarea cols='40' rows='1' wrap='off'" + "\n" +
					"			readonly='readonly' title='email'>" + rs.getString("email") + "</textarea>" + "\n" +
					"		<textarea cols='7' rows='1' wrap='off'" + "\n" +
					"			name='money' title='money'>" + rs.getInt("money") + "</textarea>" + "\n" +
					"		<br/>" + "\n" +
					"		<textarea cols='50' rows='3' wrap='wrap'" + "\n" +
					"			readonly='readonly' title='consume_log'>" + rs.getString("log") + "</textarea>" + "\n" +
					"	</div>" + "\n" +
					"	<input id='usrs_submit' class='label task' type='submit' " + "\n" +
					"		title='update Credit & Money' value=''/>" + "\n" +
					"</form>" + "\n" +
					"<div class='clear'></div>"
				);
			}
			
			out.flush();
			
			stmt.close();
			con.close();
			
		} catch (IOException | SQLException e) {
			e.printStackTrace();
		}
	}
}
